package com.example.demo.controller;


import com.example.demo.dto.*;
import com.example.demo.entity.Admin;
import com.example.demo.entity.User;
import com.example.demo.service.AuthLogService;
import com.example.demo.service.AuthService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/auth")
public class AuthController {

    @Autowired
    private AuthService authService;
    @Autowired
    private AuthLogService authLogService;

    @PostMapping("/login")
    public ResponseEntity<Object> login(@RequestBody LoginRequest loginRequest) {
        boolean success =  authService.login(loginRequest);
        Map<String, Boolean> response = new HashMap<>();
        response.put("success", success);
        return new ResponseEntity<>(response, success ? HttpStatus.OK: HttpStatus.UNAUTHORIZED);
    }

    @PostMapping("/reset-password")
    public ResponseEntity<Boolean> resetPassword(@RequestBody ResetPasswordRequest resetPasswordRequest){
        boolean success =  authService.resetPassword(resetPasswordRequest);
        return new ResponseEntity<>(success, success ? HttpStatus.OK: HttpStatus.UNAUTHORIZED);
    }
    @PostMapping("/register-user")
    public  ResponseEntity<User> registerUser(@RequestBody User user){
        User registerUser = authService.registerUser(user);
        return new ResponseEntity<>(registerUser, HttpStatus.OK);
    }

    @PostMapping("/register-admin")
    public ResponseEntity<Admin> registerAdmin(@RequestBody Admin admin){
        Admin registerAdmin = authService.registerAdmin(admin);
        return  new ResponseEntity<>(registerAdmin, HttpStatus.OK);
    }
    @PostMapping("/listUsers")
    public  ResponseEntity<List<User>> listUsers() {
        List<User> userList = authService.findAllUsers();
        return new ResponseEntity<>(userList, HttpStatus.OK);
    }
    @PostMapping("/listAuthLogs")
    public  ResponseEntity<List<AuthLogDTO>> listAuthLogs(){
        List<AuthLogDTO> logList = authLogService.findAllAuthLogs();
        return  new ResponseEntity<>(logList, HttpStatus.OK);
    }
    @PostMapping("/log/list")
    public LogResponse getLogs(@RequestBody LogQueryRequest request) {
        return authLogService.getLogs(request);
    }
    @PostMapping("/updateUserStatus")
    public ResponseEntity<Void> updateUserStatus(@RequestBody Map<String,Object> request){
        authService.updateUserSearch(Integer.valueOf(request.get("job_num").toString()),(Boolean) request.get("search"));
        return new ResponseEntity<>(HttpStatus.OK);
    }

}